package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.View
import com.chad.library.adapter.base.BaseSectionMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.PaperTrailSection
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.item_paper_finished.view.*
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_average_score
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_comment
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_detail
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_finished
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_high_score
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_papername
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_total
import kotlinx.android.synthetic.main.item_paper_go_on.view.*
import kotlinx.android.synthetic.main.item_paper_trail_header.view.tv_time
import kotlinx.android.synthetic.main.item_paper_unpublished.view.*
import kotlinx.android.synthetic.main.item_paper_unpublished.view.tv_effective
import kotlinx.android.synthetic.main.item_paper_unpublished.view.tv_full_score
import kotlinx.android.synthetic.main.item_paper_unpublished.view.tv_klg
import kotlinx.android.synthetic.main.item_paper_unpublished.view.tv_klg_rate
import kotlinx.android.synthetic.main.item_paper_unpublished.view.tv_matchrate
import kotlinx.android.synthetic.main.item_paper_unpublished.view.tv_publish
import kotlinx.android.synthetic.main.item_school_paper.view.*
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_publish as tv_publish1
import kotlinx.android.synthetic.main.item_paper_go_on.view.tv_comment_num as tv_comment_num1
import kotlinx.android.synthetic.main.item_paper_go_on.view.tv_comment_total_num as tv_comment_total_num1
import kotlinx.android.synthetic.main.item_paper_go_on.view.tv_publish as tv_publish1

/**
 *@author Admin
 *@date 2021/2/1
 *
 */
class PaperTrailListAdapter(sectionHeadResId: Int, data: MutableList<PaperTrailSection>,
                            private val isNeedRePublish: Boolean) :
        BaseSectionMultiItemQuickAdapter<PaperTrailSection, BaseViewHolder>(sectionHeadResId,
                data) {

    init {
        addItemType(1, R.layout.item_paper_finished)
        addItemType(2, R.layout.item_paper_go_on)
        addItemType(3, R.layout.item_paper_unpublished)
    }

    @SuppressLint("SimpleDateFormat", "SetTextI18n")
    override fun convertHead(helper: BaseViewHolder?, item: PaperTrailSection?) {
        with(helper!!.itemView) {
            val format = SimpleDateFormat("yyyy/MM/dd")
            val c = Calendar.getInstance()
            format.parse(item!!.header).also { c.time = it } //判断方法
            val dayForWeek = when (c[Calendar.DAY_OF_WEEK]) {
                2 -> "星期一"
                3 -> "星期二"
                4 -> "星期三"
                5 -> "星期四"
                6 -> "星期五"
                7 -> "星期六"
                else -> {
                    "星期天"
                }
            }
            tv_time.text = "$dayForWeek ${item.header.replace("/", ".")}"
        }
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: PaperTrailSection?) {
        with(helper) {
            addOnClickListener(R.id.tv_comment)
            addOnClickListener(R.id.tv_detail)
            addOnClickListener(R.id.tv_publish)
            addOnClickListener(R.id.tv_submited)
            addOnClickListener(R.id.tv_total)
            addOnClickListener(R.id.tv_finished)
            addOnClickListener(R.id.tv_commented)
            addOnClickListener(R.id.tv_comment_num)
            addOnClickListener(R.id.tv_comment_total_num)
            addOnClickListener(R.id.tv_time)
            addOnClickListener(R.id.iv_hastencall)
        }
        with(helper.itemView) {
            val paperName = item!!.t.PaperName.replace("（", "(").replace("）", ")")
            val str = SpannableStringBuilder(paperName)
            val span = ForegroundColorSpan(Color.parseColor("#999999"))
            str.setSpan(span, paperName.lastIndexOf("("), paperName.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            tv_papername.text = str
            if (helper.itemViewType == 1 || helper.itemViewType == 2) {
                tv_publish.visibility = if (isNeedRePublish) View.VISIBLE else View.GONE
                if (item.t.EvalCount == 0) {
                    tv_comment.alpha = 0.3f
                } else {
                    tv_comment.alpha = 1.0f
                }
                tv_average_score.text = "平均分:${item.t.AverageScore}"
                tv_high_score.text = "最高分:${item.t.HighestScore}"
                tv_finished.text = item.t.SubmitCount.toString()
                tv_total.text = "/${item.t.StuCount}"
                tv_comment_num.text = item.t.CompleteEvalCount.toString()
                tv_comment_total_num.text = "/${item.t.StuCount}"
                if (helper.itemViewType == 2) {
                    if (item.t.State == 2) {
                        tv_detail.alpha = 0.3f
                    } else {
                        tv_detail.alpha = 1.0f
                    }
                }
                tv_time.text = item.t.StartTime.replace("-", ".")
                        .let {
                            it.substring(0, it.lastIndexOf(":"))
                        } + "~" + item.t.EndTime.replace(
                        "-", ".").let { it.substring(0, it.lastIndexOf(":")) }
            } else if (helper.itemViewType == 3) {
//                if (item.t.Layer == 3 || item.t.Layer == 4) {
//                    tv_high_score.visibility = View.GONE
//                    tv_effective.visibility = View.GONE
//                    textView11.visibility = View.GONE
//                    textView14.visibility = View.GONE
//                } else {
//                    tv_average_score.visibility = View.VISIBLE
//                    tv_high_score.visibility = View.VISIBLE
//                    tv_effective.visibility = View.VISIBLE
//                    textView11.visibility = View.VISIBLE
//                    textView12.visibility = View.VISIBLE
//                    textView14.visibility = View.VISIBLE
//                }
                if(item.t.ZsdExamples.isEmpty()){
                    tv_klg.text="核心考点：--"
                }else {
                    val spannable = SpannableStringBuilder(
                            "核心考点：${item.t.ZsdExamples.subList(0, 2)
                                    .joinToString("、")}...（共${item.t.ImportantZsdCount}个）")
                    spannable.setSpan(ForegroundColorSpan(Color.parseColor("#ff9900")),
                            spannable.lastIndexOf("共") + 1, spannable.length - 2,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                    tv_klg.text = spannable
                }
                tv_klg_rate.text = "${item.t.PaperQualityAnalyze.PaperKnowledgeRate}%"
                tv_matchrate.text = "${item.t.PaperQualityAnalyze.PaperMatchRate}%"
                tv_effective.text = "${item.t.PaperQualityAnalyze.PaperEffective}%"
                tv_full_score.text = HtmlUtils.setText(
                        "满分：<b><font color='#333333'>${BigDecimal(item.t.FullScore).stripTrailingZeros()
                                .toPlainString()}分</font></b>")
                tv_difficult.text = HtmlUtils.setText(
                        mContext.resources.getString(R.string.difficult,
                                item.t.PaperQualityAnalyze.PaperDifficulty))
                tv_discrimination.text = HtmlUtils.setText(
                        mContext.resources.getString(R.string.discrimination,
                                item.t.PaperQualityAnalyze.PaperDistinguish))
                tv_exposure.text = HtmlUtils.setText(mContext.resources.getString(R.string.exposure,
                        BigDecimal(
                                item.t.PaperQualityAnalyze.PaperExposureRate).stripTrailingZeros()
                                .toPlainString()))
            }
        }
    }
}